//
//  DocumentViewController.swift
//  Auscultation
//
//  Created by 张文博 on 2021/4/6.
//

import UIKit
import JXSegmentedView

class DocumentViewController: BaseVC,UITextFieldDelegate {
    var pageNum: Int = 1
    var pageSize: Int = 10

    var dataSource = [HomeCourseModel?]()
    
    lazy var table: UITableView = {
        let table = UITableView(frame: .zero, style: .grouped)
        view.addSubview(table)
        table.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalToSuperview().offset(UIDevice.lgf_NavBarH())
        }
        table.delegate = self
        table.dataSource = self
        table.register(UINib.init(nibName: "RecomcCourseCell", bundle: Bundle.main), forCellReuseIdentifier: "RecomcCourseCell")
        table.separatorStyle = .none
        table.backgroundColor = .clear
        return table
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        // 搜索 bar
        let search_bar = UIView.init()
        search_bar.backgroundColor = UIColor.white.withAlphaComponent(0.32)
        search_bar.layer.cornerRadius = 4
        search_bar.backgroundColor = UIColor.init(lgf_HexString: "F3F3F3")
        view.addSubview(search_bar)
        search_bar.snp.makeConstraints { (make) in
            make.top.equalTo(view).offset(20)
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-15)
            make.height.equalTo(40)
        }
        let search_image = UIImageView()
        search_image.image = UIImage(named: "UIsheji_menjinxitong-17")
        search_bar.addSubview(search_image)
        search_image.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(12)
            make.width.height.equalTo(16)
        }
        let search_text = UITextField.init()
        search_text.attributedPlaceholder = NSAttributedString.init(string:"请输入您要搜索的文档内容", attributes: [NSAttributedString.Key.foregroundColor:UIColor.init(lgf_HexString: "A0A0A0")!])
        search_text.textColor = UIColor.black
        search_text.tintColor = UIColor.black
        search_text.font = UIFont.systemFont(ofSize: 12)
        search_text.delegate = self
        search_bar.addSubview(search_text)
        search_text.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(search_image.snp.right).offset(12)
            make.right.equalToSuperview().offset(-60)
            make.height.equalToSuperview()
        }
        
        let sx = UILabel.init()
        sx.text = "搜索"
        sx.font = UIFont.systemFont(ofSize: 14)
        sx.textAlignment = .center
        sx.textColor = UIColor.init(lgf_HexString: "A0A0A0")!
        sx.isUserInteractionEnabled = true
        sx.lgf_AddTap(target: self, action: #selector(VideoViewController.videoSearch(btn:)))
        view.addSubview(sx)
        sx.snp.makeConstraints { (make) in
            make.centerY.equalTo(search_bar)
            make.right.equalToSuperview().offset(-15)
            make.width.equalTo(50)
            make.height.equalTo(50)
        }
        
        let lineView = UIView()
        lineView.backgroundColor = UIColor.init(lgf_HexString: "A0A0A0")!
        view.addSubview(lineView)
        lineView.snp.makeConstraints { (make) in
            make.right.equalTo(sx.snp.left)
            make.width.equalTo(1)
            make.height.equalTo(20)
            make.centerY.equalTo(search_bar)
        }
        view.addSubview(table)
        table.snp.makeConstraints { (make) in
            
            make.left.right.equalToSuperview()
            make.top.equalTo(search_bar.snp.bottom).offset(10)
            make.bottom.equalToSuperview().offset(-UIDevice.lgf_BottomSafeAreaH())
        }

    }
    
}

extension DocumentViewController: JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return view
    }
}


extension DocumentViewController : UITableViewDelegate, UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return 1
        }
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "RecomcCourseCell") as! RecomcCourseCell
        guard let model = dataSource[indexPath.row] else { return cell }
            cell.model = model
            return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//        guard let model = dataSource[indexPath.row] else { return }
        let vc = WebVC()
//        vc.titleString = model.title
//        vc.url = model.filePath
        self.navigationController?.pushViewController(vc, animated: true)

    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 110
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return CGFloat.leastNormalMagnitude
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        
        return CGFloat.leastNormalMagnitude
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView()
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let footer = UIView()
        footer.backgroundColor = .white
        return footer
    }
}


extension DocumentViewController {
    func loadData() {
        let paramet  = [
            "pageNo" : pageNum.description,
            "pageSize" : pageSize.description
        ]
        EWERequest.lgf("course", paramet, .get) {  (data) in
            guard let result = (data["result"] as? [Any]?) else { return  }
            if self.pageNum == 1 {
                self.dataSource.removeAll()
                self.table.mj_header?.endRefreshing()
            }
            
            let arr = Array<HomeCourseModel>.deserialize(from: result)
            if arr?.count == 0 {
                self.table.mj_footer?.endRefreshingWithNoMoreData()
            }else{
                self.table.mj_footer?.endRefreshing()
            }
            self.dataSource += arr ?? []
            self.table.reloadData()
        } _: {  (error) in
            SVProgressHUD.showError(error)
        }

    }

}
